package com.zhudoo.zhilian.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * JsonUtil工具集
 *
 * @author zhudao
 */
public class JsonUtil {


    public static Map<String, Object> object2Map(Object obj, SerializerFeature... serializerFeature) {
        if (obj == null) {
            return new HashMap<>(10);
        }
        String json = object2JSON(obj, SerializerFeature.WriteDateUseDateFormat,
                SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullBooleanAsFalse, SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNonStringKeyAsString);
        Map<String, Object> map = JSON.parseObject(json, Map.class);
        return map;
    }

    public static List<Map<String, Object>> objects2List(Object obj, SerializerFeature... serializerFeature) {
        if (obj == null) {
            return new ArrayList<>();
        }
        String json = object2JSON(obj, serializerFeature);
        List<Map<String, Object>> rows = JSON.parseObject(json, List.class);
        return rows;
    }

    public static String object2JSON(Object obj, SerializerFeature... serializerFeature) {
        if (obj == null) {
            return "{}";
        }
        return JSON.toJSONString(obj, serializerFeature);
    }

    public static String object2JSON(Object obj) {
        if (obj == null) {
            return "{}";
        }
        return JSON.toJSONString(obj, SerializerFeature.WriteDateUseDateFormat,
                SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullBooleanAsFalse, SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNonStringKeyAsString
        );
    }

    public static JSONObject object2JSONObject(Object obj) {
        return JSON.parseObject(object2JSON(obj));

    }

    public static <T> T json2Object(String json, Class<T> clazz) {
        if (json == null || json.isEmpty()) {
            return null;
        }
        return JSON.parseObject(json, clazz);
    }

    public static <T> T json2Reference(String json, TypeReference<T> reference) {
        if (json == null || json.isEmpty()) {
            return null;
        }
        return JSON.parseObject(json, reference);
    }

    public static <T> T json2Reference(String json, TypeReference<T> reference, Feature... features) {
        if (json == null || json.isEmpty()) {
            return null;
        }
        return JSON.parseObject(json, reference, features);
    }

    public static <T> T map2Object(Map<String, Object> map, Class<T> clazz) {
        return json2Object(JSON.toJSONString(map), clazz);
    }

    public static JSONObject string2JSON(String str) {
        return JSON.parseObject(str);
    }

    public static JSONArray string2JSONArray(String str) {
        return JSON.parseArray(str);
    }
}